<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .container{
            display: flex;
            width:170px;
            justify-content: space-between;

        }
        .container > div{
            width:40px;
            height: 40px;
            background-color: #ccc;
            text-align: center;
            line-height: 40px;
            color:white;
        }
    </style>
    <script>
        function calc(flag){
            var inputDom=document.querySelector("input");//获取元素的DOM对象
            var val=inputDom.value*1;//获取当前文本框的值，DOM对象.value
            if(flag=="+"){
                val=val+1;
            }else{
               if(val>0){
                    val=val-1;
               }else{
                    alert("你咋这么抠门，不能再减啦！！！");
               }
            }
            //将计算好的值重新赋值给input元素
            inputDom.value=val;
        }
    </script>
</head>
<body>
    <div class="container">
        <div onclick="calc('-')">-</div> 
        <input type="text" value="0" size="3">
        <div onclick="calc('+')">+</div>
    </div>
 
</body>
</html>